#include <stdio.h>
#include <stdlib.h>

#define SIZE 40

typedef struct Student
{
	char ID[20];
	char name[30];
	int age;            /* 4 */
	double score;       /* 8 */
} Student;



int main ( int argc, char const *argv[] ) {
    int num = 0, i = 0;
    Student stu1[SIZE];
    Student stu2[SIZE];
    FILE *fp1;
    fp1 = fopen("/tmp/Info.dat","wb+");
    if ( !fp1 ) {
        printf("Err Create\n");
        return EXIT_FAILURE;
    }
    printf("Please input the number of the students:\n");
    scanf("%d",&num);
    for ( i = 0; i < num; i++ ) {
        printf("Please input the %dth student's Id name age score:\t",i+1);
        scanf("%s %s %d %lf",stu1[i].ID, stu1[i].name, &stu1[i].age, &stu1[i].score);
    }
    fwrite( stu1, sizeof(Student), (i + 1), fp1 );
    printf("Input Complete\n");
    fclose(fp1);
    // rewind(fp);
    FILE *fp2;
    fp2 = fopen("/tmp/Info.dat","rb");
    if ( !fp2 ) {
        printf("Err Create2\n");
        exit(-9);
    }
    for ( i = 0; i < num; i++) {
        fread( &stu2[i], sizeof(Student), 1, fp2);
    }
    printf("Read complete\n");
    for ( i = 0; i < num; i++ ) {
        printf("%s\t%s\t%d\t%f\n",stu2[i].ID, stu2[i].name, stu2[i].age, stu2[i].score);
    }
    printf("Print complete\t\t\t%d recordings\nHappy New Year!\n",num);
    fclose(fp2);
    return 0;
}